package com.jgsu.farm.service.impl;

import com.jgsu.farm.entity.MatchImage;
import com.jgsu.farm.entity.Question;
import com.jgsu.farm.entity.User;
import com.jgsu.farm.exception.DataAddException;
import com.jgsu.farm.exception.DataNotExistException;
import com.jgsu.farm.mapper.MatchImageMapper;
import com.jgsu.farm.mapper.QuestionMapper;
import com.jgsu.farm.mapper.UserMapper;
import com.jgsu.farm.mapper.sql.MatchImageMapperSql;
import com.jgsu.farm.service.QuestionService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class QuestionServiceImpl implements QuestionService {
    @Autowired
    QuestionMapper questionMapper;
    @Autowired
    UserMapper userMapper;
    @Autowired
    MatchImageMapper matchImageMapper;
    @Autowired
    MatchImageMapperSql matchImageMapperSql;

    @Override
    public void add(Question question) {
        User user = userMapper.selectByPrimaryKey(question.getUserid());
        if (user == null) {
            throw new DataAddException("604", "插入的userid在user表中没有，请先插入到user表");
        } else if (question.getImage() == null || question.getImage() == 0) {
            questionMapper.insert(question);
        } else {
            List<MatchImage> matchImageList = matchImageMapperSql.selectByMatchId(question.getImage());
            if (!matchImageList.isEmpty()) {
                questionMapper.insert(question);
            }else {
                throw new DataAddException("604", "插入的image在matchimage表中没有相应的matchimageid，请先插入到matchimage表");
            }
        }
    }

    @Override
    public void delete(Integer questid) {
        Integer questionId = questionMapper.deleteByPrimaryKey(questid);
        if (questionId == 0 || questionId == null) {
            throw new DataNotExistException("603", "不存在该id,咨询信息删除失败");
        }
    }

    @Override
    public PageInfo<Question> selectAll(int page, int pagesize) {
        PageHelper.startPage(page, pagesize);
        List<Question> questions = questionMapper.selectAll();
        PageInfo<Question> pageInfoQuestionList = new PageInfo<Question>(questions);
        return pageInfoQuestionList;
    }

    @Override
    public void update(Question question) {
        Question questionId = questionMapper.selectByPrimaryKey(question.getQuestid());
        if (questionId == null) {
            throw new DataNotExistException("603", "数据更新失败，不存在相应咨询id");
        } else {
            if (question.getImage() == null || question.getImage() == 0) {
                questionMapper.updateByPrimaryKey(question);
            } else {
                List<MatchImage> matchImageList = matchImageMapperSql.selectByMatchId(question.getImage());
                if (!matchImageList.isEmpty()) {
                    questionMapper.updateByPrimaryKey(question);
                } else {
                    throw new DataNotExistException("603", "数据更新失败，不存在相应imageId");
                }
            }
        }
    }
}
